

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Database Versioning and Migration &mdash; Cogent Viewer 0.1 documentation</title>
    
    <link rel="stylesheet" href="_static/rtd.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Cogent Viewer 0.1 documentation" href="index.html" />
    <link rel="up" title="Cruft and Notes" href="cruft.html" />
    <link rel="next" title="JSON Interface" href="jsonFunctions.html" />
    <link rel="prev" title="My Install Notes" href="dansInstallNotes.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="jsonFunctions.html" title="JSON Interface"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="dansInstallNotes.html" title="My Install Notes"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="cruft.html" accesskey="U">Cruft and Notes</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="database-versioning-and-migration">
<h1>Database Versioning and Migration<a class="headerlink" href="#database-versioning-and-migration" title="Permalink to this headline">¶</a></h1>
<p>This document outlines modifications to the database and the process&#8217;s needed to perfor versioning.</p>
<div class="section" id="requirements">
<h2>Requirements<a class="headerlink" href="#requirements" title="Permalink to this headline">¶</a></h2>
<p>The database migration code makes use of sqlalchemy-migrate [<a class="reference external" href="http://code.google.com/p/sqlalchemy-migrate/">http://code.google.com/p/sqlalchemy-migrate/</a>]</p>
<dl class="docutils">
<dt>Install using pip (or easy_install)::</dt>
<dd>pip install sqlalchemy-migrate</dd>
</dl>
</div>
<div class="section" id="setup">
<h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline">¶</a></h2>
<dl class="docutils">
<dt>Create a new repository for the database::</dt>
<dd>#migrate create &lt;repo-name&gt; &#8220;&lt;desc&gt;&#8221;
migrate create cogent-db-repo &#8220;Cogent Database&#8221;</dd>
<dt>Then version control the current DB</dt>
<dd>python /cogent-db-repo/manage.py version_control &lt;sqlite db string&gt; cogent-db-repo</dd>
<dt>We can also setup a quick access script with the addresses preconfigured using::</dt>
<dd>migrate manage manage.py &#8211;repository=cogent-db-repo/ &#8211;url=mysql://root:Ex3lS4ga&#64;localhost/ch</dd>
</dl>
</div>
<div class="section" id="making-changes">
<h2>Making Changes<a class="headerlink" href="#making-changes" title="Permalink to this headline">¶</a></h2>
<dl class="docutils">
<dt>First setup a new changeset::</dt>
<dd>python manage.py script &#8220;scriptName&#8221;</dd>
</dl>
<p>This create a file in &#8216;&#8217;&#8216;repo/versions/&lt;vurrent&gt;_&lt;script&gt;&#8217;&#8216;&#8217; we edit this to make our changes.</p>
<p>The script has two predefiend funcions &#8216;&#8217;upgrade()&#8217;&#8217; and &#8216;&#8217;downgrade()&#8217;&#8217; we need to make sure these are complete, otherwise there could be inconsistencys in the way things work:</p>
<div class="highlight-python"><pre>#from sqlalchemy import *
import sqlalchemy
from migrate import *

meta = sqlalchemy.MetaData()

"""
Association object to link rooms to Nodes
This means we can have a many to many link between rooms and nodes,
while still maintianing whatever functionality the cogent software relies on
"""
  roomNodes = sqlalchemy.Table('room-nodes',
                               meta,
                               sqlalchemy.Column('roomId',
                                                 sqlalchemy.Integer,
                                                 sqlalchemy.ForeignKey('Room.id')),
                               sqlalchemy.Column('nodeId',
                                                 sqlalchemy.Integer ,
                                                 sqlalchemy.ForeignKey('Node.id'))
                               )

  def upgrade(migrate_engine):
      # Upgrade operations go here. Don't create your own engine; bind
      # migrate_engine to your metadata
      meta.bind = migrate_engine

      #Reflect any tables with FK's etc
      Room = sqlalchemy.Table("Room",meta,autoload=True)
      Node = sqlalchemy.Table("Node",meta,autoload=True)

      roomNodes.create()

  def downgrade(migrate_engine):
      # Operations to reverse the above upgrade go here.
      meta.bind = migrate_engine
      roomNodes.drop()</pre>
</div>
<p>And test the changes:</p>
<div class="highlight-python"><pre>python manage.py test</pre>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Database Versioning and Migration</a><ul>
<li><a class="reference internal" href="#requirements">Requirements</a></li>
<li><a class="reference internal" href="#setup">Setup</a></li>
<li><a class="reference internal" href="#making-changes">Making Changes</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="dansInstallNotes.html"
                        title="previous chapter">My Install Notes</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="jsonFunctions.html"
                        title="next chapter">JSON Interface</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/dbVersion.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="jsonFunctions.html" title="JSON Interface"
             >next</a> |</li>
        <li class="right" >
          <a href="dansInstallNotes.html" title="My Install Notes"
             >previous</a> |</li>
        <li><a href="index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="cruft.html" >Cruft and Notes</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, Daniel Goldsmith.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>